package com.ruoyi.patrol.service.impl;

import com.ruoyi.common.exception.CustomException;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.patrol.domain.PtSchoolGroup;
import com.ruoyi.patrol.mapper.PtLineGuardMapper;
import com.ruoyi.patrol.mapper.PtLineStaffMapper;
import com.ruoyi.patrol.mapper.PtLineTaskMapper;
import com.ruoyi.patrol.mapper.PtSchoolGroupMapper;
import com.ruoyi.patrol.service.IPtSchoolGroupService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 学校小组Service业务层处理
 *
 * @author lkm
 * @date 2023-05-13
 */
@Service
public class PtSchoolGroupServiceImpl implements IPtSchoolGroupService {
    @Autowired
    private PtSchoolGroupMapper ptSchoolGroupMapper;

    @Autowired
    private PtLineTaskMapper ptLineTaskMapper;

    /**
     * 查询学校小组
     *
     * @param groupId 学校小组ID
     * @return 学校小组
     */
    @Override
    public PtSchoolGroup selectPtSchoolGroupById(Long groupId) {
        return ptSchoolGroupMapper.selectPtSchoolGroupById(groupId);
    }

    /**
     * 查询学校小组列表
     *
     * @param ptSchoolGroup 学校小组
     * @return 学校小组
     */
    @Override
    public List<PtSchoolGroup> selectPtSchoolGroupList(PtSchoolGroup ptSchoolGroup) {
//        ptSchoolGroup.setGroupSchoolId(SecurityUtils.getSchoolId());
        if (ptSchoolGroup.getGroupSchoolId() == null) {
            ptSchoolGroup.setGroupSchoolId(SecurityUtils.getSchoolId());
        }
        return ptSchoolGroupMapper.selectPtSchoolGroupList(ptSchoolGroup);
    }

    @Override
    public List<PtSchoolGroup> selectPtSchoolGroupListx(PtSchoolGroup ptSchoolGroup) {
        ptSchoolGroup.setGroupSchoolId(SecurityUtils.getSchoolId());
        return ptSchoolGroupMapper.selectPtSchoolGroupListx(ptSchoolGroup);
    }

    /**
     * 新增学校小组
     *
     * @param ptSchoolGroup 学校小组
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int insertPtSchoolGroup(PtSchoolGroup ptSchoolGroup) {
        ptSchoolGroup.setCreateTime(DateUtils.getNowDate());
        Integer x1 = getRepeat(ptSchoolGroup);
        if (x1 != null) return x1;
        int x = 0;
        ptSchoolGroupMapper.insertPtSchoolGroup(ptSchoolGroup);
        x = ptSchoolGroupMapper.updatePtSchoolGroupUser(ptSchoolGroup);
        return x;
    }


    private Integer getRepeat(PtSchoolGroup ptSchoolGroup) {
        Integer integer = ptSchoolGroupMapper.queryForDuplicateGroupNames(ptSchoolGroup);
        if (integer > 0) return -3;
        return null;
    }

    /**
     * 修改学校小组
     *
     * @param ptSchoolGroup 学校小组
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int updatePtSchoolGroup(PtSchoolGroup ptSchoolGroup) {
        Integer x1 = getRepeat(ptSchoolGroup);
        if (x1 != null) return x1;
        ptSchoolGroup.setUpdateTime(DateUtils.getNowDate());
        ptSchoolGroupMapper.updatePtSchoolGroup(ptSchoolGroup);
        Long groupId = ptSchoolGroup.getGroupId();
        List userlist = ptSchoolGroup.getUserlist();
        List userlistx = ptSchoolGroup.getUserlistx();
        int i = 0;
        if (!userlistx.isEmpty()) {
            ptSchoolGroup.setUserlist(userlistx);
            ptSchoolGroup.setGroupId(null);
             i = ptSchoolGroupMapper.updatePtSchoolGroupUser(ptSchoolGroup);
        }
        if (!userlist.isEmpty()) {
            ptSchoolGroup.setUserlist(userlist);
            ptSchoolGroup.setGroupId(groupId);
             i = ptSchoolGroupMapper.updatePtSchoolGroupUser(ptSchoolGroup);
        }

        return i;
    }

    /**
     * 批量删除学校小组
     *
     * @param groupIds 需要删除的学校小组ID
     * @return 结果
     */
    @Override
    public int deletePtSchoolGroupByIds(Long[] groupIds) {
        return ptSchoolGroupMapper.deletePtSchoolGroupByIds(groupIds);
    }

    /**
     * 删除学校小组信息
     *
     * @param groupId 学校小组ID
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int deletePtSchoolGroupById(Long groupId) {
        PtSchoolGroup ptSchoolGroup = selectPtSchoolGroupById(groupId);
        long count;
        if (ptSchoolGroup.getGroupType()) {
            count = ptLineGuardMapper.selectTodayOverCount(groupId);
        } else {
            count = ptLineStaffMapper.selectTodayOverCount(groupId);
        }
        if (count > 0) {
            throw new CustomException("今日或未来已进行排班，请检查后删除");
        }
        List list = ptSchoolGroupMapper.selectPtSchoolGroupUserList(ptSchoolGroup);
        ptSchoolGroup.setUserlist(list);
        ptSchoolGroup.setGroupId(null);
        ptLineTaskMapper.deletePtLineTaskByGroupId(groupId);
        ptSchoolGroupMapper.updatePtSchoolGroupUser(ptSchoolGroup);
        return ptSchoolGroupMapper.deletePtSchoolGroupById(groupId);
    }

    @Autowired
    private PtLineGuardMapper ptLineGuardMapper;

    @Autowired
    private PtLineStaffMapper ptLineStaffMapper;
}